package com.version1.diabeco.ui.mapas;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.version1.diabeco.R;
import com.version1.diabeco.data.mapas.GeocercaDaoImpl;
import com.version1.diabeco.model.mapas.Geocerca;

public class ActivityGeocercaList extends Activity {

	private static final int REQUEST_CODE = 1237;

	private List<Geocerca> geocercas;

	public List<Geocerca> getGeocercas() {
		return geocercas;
	}

	public void setGeocercas(List<Geocerca> geocercas) {
		this.geocercas = geocercas;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lista_registros_geocerca);

		this.cargarPantalla();
	}

	private void cargarPantalla() {

		this.setGeocercas(GeocercaDaoImpl.getInstance(this).selectAll());

		List<Geocerca> geocercas = this.getGeocercas();

		if (!geocercas.isEmpty()) {
			ListView geocercaList = (ListView) findViewById(R.id.list_geocercas);

			ArrayAdapter<Geocerca> adapter = new ArrayAdapter<Geocerca>(this, android.R.layout.simple_list_item_1, geocercas);
			geocercaList.setAdapter(adapter);

			geocercaList.setOnItemLongClickListener(new OnItemLongClickListener() {

				public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

					ArrayAdapter<Geocerca> arrayAdapter = (ArrayAdapter<Geocerca>) ((ListView) parent).getAdapter();
					Geocerca geocerca = arrayAdapter.getItem(position);

					GeocercaDaoImpl.getInstance(ActivityGeocercaList.this).deleteById(geocerca.getId());
					getGeocercas().remove(geocerca);

					arrayAdapter.notifyDataSetChanged();

					return true;
				}
			});
		} else {
			// TODO inform the user there are no comidas taken
		}
	}

}
